<%@page import="com.carnation.bean.User"%>
<%@page import="com.carnation.utils.SessionInfo"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="../taglibs.jsp"%>
<%@ include file="../easyuiLib.jsp"%>
<% SessionInfo sessionInfo = (SessionInfo)request.getSession().getAttribute("sessionInfo");
	String operation = "'<a href=\"#\" onclick=\"down()\">下载</a> '";
	User user = sessionInfo.getUser();
	if(user.getRole().equals("manager")){
		operation = operation + "+ '<a href=\"#\" onclick=\"del()\">删除</a>'";
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" charset="utf-8">
	function html_encode(str)
	{
 	 	var s = "";
  		if (str.length == 0) return "";
  		s = str.replace(/&/g, ">");
  		s = s.replace(/</g, "<");
  		s = s.replace(/>/g, ">");
  		s = s.replace(/ /g, " ");
  		s = s.replace(/\'/g, "'");
  		s = s.replace(/\"/g, "\"");
  		s = s.replace(/\n/g, "<br>");
  		return s;
	}

	$(function(){
		$('#filetableid').datagrid({
			url:'${ctx}/FileInfo?id=<%=request.getParameter("id")%>',
			title:'文件列表',
			fit:true,
			border:false,
			nowarp:true,
			fitColumns:false,
			singleSelect:true,
			rownumbers:true,
			columns:[[
			   {field:'catalogId',hidden:true,title:'所属目录ID'},
			   {field:'name',title:'文件名称',width:'20%',align:'center'},
			   {field:'size',title:'文件大小',hidden:true,formatter:function(value,row,index){
				   return value/1024/1024;
			   }},
			   {field:'type',title:'类型',hidden:true},
			   {field:'uploadUser',title:'上传人',width:'10%'},
			   {field:'uploadTime',title:'上传时间',width:'15%'},
			   {field:'description',title:'介质描述',width:'35%',formatter:function(value,row,index){
				   var tmp = html_encode(value);
				   var div = $('<div word-wrap="break-word" word-break="normal" class="note">' +value + '</div>');
				   div.attr('title',tmp);
				   return div.prop("outerHTML");
			   }},
			   {field:'remark',title:'备注',width:'10%',formatter:function(value,row,index){
				   var tmp = html_encode(value);
				   var div = $('<div word-wrap="break-word" word-break="normal" class="note">' +value + '</div>');
				   div.attr('title',tmp);
				   return div.prop("outerHTML");
			   }},
			   {field:'something',title:'操作',width:'5%',formatter:function(value,row,index){
				   return <%=operation%>;
			   }}
			]],
			toolbar:'#tt',
			//onDblClickRow : function(index,row){
				//$('#detail').window('open');
			//},
			onLoadSuccess : function(data){
				$(".note").tooltip({
					onShow :function(){
						$(this).tooltip('tip').css({
							width:'500',
							boxShadow :'1px 1px 3px #292929'
						})
					}
				});
			}
		});
		
	});
	
	function down(){
		var row =$('#filetableid').datagrid('getSelected');
		console.log(row);
		if(row){
			var params={};
			var url = "${ctx}/downfile";
			params.name=row.name;
			params.catalogid = row.catalogId;
			_export(url,params);
		}	
	}
	
	function _export(url,parms){
    	var _form = $('#exportForm');
    	if(_form){
    		_form.remove();
    	}
        _form = $('<form id="exportForm" ' + 'action=' +url +
					' method="post" target="_blank" style="">' +
					'</form>');
		$.each(parms, function(index, val) {
		   _form.append('<input type="hidden" value=' + val+
					' name="'+index+'" id="'+index+'"/>');
		});
		$("body").append(_form);
		_form.submit();
    }
	
	function upload(){
		$('#uploadIframe')[0].src='${ctx}/toUpload?id=<%=request.getParameter("id")%>&username=<%=sessionInfo.getLoginName()%>';
		$('#up').dialog('open');
	}
	
	function filesearch(){
		var filename = $('#filename').textbox('getValue');
		var username = $('#username').textbox('getValue');
		var startTime = $('#start').datebox('getValue');
		var endTime = $('#end').datebox('getValue');
		var description = $('#filedesc').textbox('getValue');
		var remark = $('#fileremark').textbox('getValue');
		var from = new Date(Date.parse(startTime.replace(/-/g,   '/')));
		var to =  new Date(Date.parse(endTime.replace(/-/g,   '/')));
		if(from != null && to != null && from > to){
			$.messager.alert('提示','开始时间不能大于结束时间');
		}
		else{
			$('#filetableid').datagrid('load',{
				name: filename,
				user : username,
				start : startTime,
				end:endTime,
				desc : description,
				remark : remark
			});
		}
	}
	
	function fileclear(){
		$('#filename').textbox('clear');
		$('#username').textbox('clear');
		$('#start').datebox('clear');
		$('#end').datebox('clear');
		$('#filedesc').textbox('clear');
		$('#fileremark').textbox('clear');
		$('#filetableid').datagrid('load',{});
	}
	
	function del(){
		var row = $('#filetableid').datagrid('getSelected');
		console.log(row);
		if(row){
			$.ajax({
				url:'${ctx}/del',
				data: 'name=' + row.name + "&catalogId=" + row.catalogId,
				async:false,
				success: function(data){
					var json = JSON.parse(data);
					if(json.status =="200"){
						$('#filetableid').datagrid('load',{});
					}
					$.messager.alert('结果',json.msg);
				}
			});
		}
	}
	
</script>
</head>
<body>
	<div class="easyui-layout" fit="true" border="false">
		<div region="center" border="false">
			<table id="filetableid"></table>
		</div>
		<div region="north" title="过滤" style="height:100px;background:#F1F1F1;" border="false" >
			<div style="padding:5px;">
			<table>
				<tr>
					<td><span  style="float:right;font-size:10px;">文件名:</span>	</td>
					<td><input class="easyui-textbox" id="filename"/></td>
					<td><span  style="float:right;font-size:10px;">上传者:</span></td>
					<td><input class="easyui-textbox" id="username"/></td>
					<td><span style="float:right;font-size:10px;">上传时间:<input id="start" type="text" class="easyui-datebox" editable="false"/>至<input id="end" type="text" class="easyui-datebox" editable="false"/></span></td>
				</tr>
				<tr>
					<td><span  style="float:right;font-size:10px;">介质描述:</span></td>
					<td><input id="filedesc" type="text" class="easyui-textbox" /></td>
					<td><span style="float:right;font-size:10px;">备注:</span></td>
					<td><input id="fileremark" type="text" class="easyui-textbox" /></td>
					<td><span style="float:right;font-size:10px;"><input style="width:80;" onclick="filesearch()" class="easyui-linkbutton" value="搜索"  > <input style="width:80;" onclick="fileclear()" class="easyui-linkbutton" value="清空" /></span></td>
				</tr>				
			</table>
			</div>
		</div>
	</div>
	<div id="tt" style="background:#F1F1F1">
		<%if(user.getRole().equals("manager")) {%>
			<a class="easyui-linkbutton" iconCls="icon-upload" onclick="upload()">上传</a>
		<%}%>
		
		<a class="easyui-linkbutton" iconCls="icon-reload" onclick="filesearch()">刷新</a>
	</div>
	<div id="up" class="easyui-window" data-options="modal:true,width:500,height:450,closed:true,closable:true,title:'上传文件',collapsible:false,minimizable:false">
		<iframe scrolling="auto" id="uploadIframe" frameborder="0" src="" style="width:100%;height:100%;"></iframe>		
	</div>
</body>
</html>